package com.zhiche.lisa.bms.controller.fee;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.fee.FeeReward;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.pojo.vo.fee.FeeRewardConventVO;
import com.zhiche.lisa.bms.pojo.vo.fee.FeeRewardVO;
import com.zhiche.lisa.bms.service.fee.FeeRewardService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 奖励费用 前端控制器
 * </p>
 *
 * @author liuanshun
 * @since 2019-01-29
 */
@RestController
@RequestMapping(value = "/feeReward")
@Api(value = "/feeReward", description = "奖励费用列表接口", tags = {"奖励费用列表接口"})
public class FeeRewardController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FeeRewardController.class);

    @Autowired
    private FeeRewardService feeRewardService;

    @PostMapping(value = "/feeRewardList")
    @ApiOperation(value = "奖励费用列表", notes = "获取奖励费用列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<List<FeeReward>> feeRewardList(@RequestBody Map<String, Object> condition) {
        LOGGER.info("FeeRewardController.feeRewardList param: {}", condition);
        RestfulResponse<List<FeeReward>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            List<FeeReward> list = feeRewardService.selectFeeRewardList(condition);
            result.setData(list);
        } catch (BaseException e) {
            LOGGER.error("FeeRewardController.feeRewardList error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeRewardController.feeRewardList error:{}", e.getMessage());
            result.setMessage("获取奖励费用列表异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "/feeRewardPage")
    @ApiOperation(value = "奖励费用分页列表", notes = "获取奖励费用分页列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<Page<FeeRewardConventVO>> feeRewardPage(@RequestBody Page<FeeReward> param) {
        LOGGER.info("FeeRewardController.feeRewardPage param: {}", param);
        RestfulResponse<Page<FeeRewardConventVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            Page<FeeRewardConventVO> list = feeRewardService.selectFeeRewardPage(param);
            result.setData(list);
        } catch (BaseException e) {
            LOGGER.error("FeeRewardController.feeRewardPage error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeRewardController.feeRewardPage error:{}", e.getMessage());
            result.setMessage("获取奖励费用列表异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "/examine")
    @ApiOperation(value = "奖励费用审核", notes = "获取奖励费用审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<Object> examine(@RequestBody CommonVO<Long> feeRewardCommonVO
            , @RequestHeader("Authorization") String token) {
        LOGGER.info("FeeRewardController.examine param: {}", feeRewardCommonVO);
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            feeRewardService.examine(feeRewardCommonVO.getEntityParams(), token);
        } catch (BaseException e) {
            LOGGER.error("FeeRewardController.examine error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeRewardController.examine error:{}", e.getMessage());
            result.setMessage("获取奖励审核异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "/hang")
    @ApiOperation(value = "奖励费用挂起", notes = "获取奖励费用挂起", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<Object> hang(@RequestBody CommonVO<Long> feeRewardCommonVO) {
        LOGGER.info("FeeRewardController.hang param: {}", feeRewardCommonVO);
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            feeRewardService.hang(feeRewardCommonVO.getEntityParams());
        } catch (BaseException e) {
            LOGGER.error("FeeRewardController.hang error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeRewardController.hang error:{}", e.getMessage());
            result.setMessage("获取奖励费用挂起异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "/unhang")
    @ApiOperation(value = "奖励费用取消挂起", notes = "奖励费用取消挂起", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<Object> unhang(@RequestBody CommonVO<Long> feeRewardCommonVO) {
        LOGGER.info("FeeRewardController.hang param: {}", feeRewardCommonVO);
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            feeRewardService.unhang(feeRewardCommonVO.getEntityParams());
        } catch (BaseException e) {
            LOGGER.error("FeeRewardController.unhang error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeRewardController.unhang error:{}", e.getMessage());
            result.setMessage("获取奖励费用挂起异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "奖励费用删除", notes = "获取奖励费用删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<Object> delete(@RequestBody CommonVO<Long> feeRewardCommonVO) {
        LOGGER.info("FeeRewardController.hang param: {}", feeRewardCommonVO);
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            feeRewardService.delete(feeRewardCommonVO.getEntityParams());
        } catch (BaseException e) {
            LOGGER.error("FeeRewardController.hang error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeRewardController.hang error:{}", e.getMessage());
            result.setMessage("获取奖励费用挂起异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "/release")
    @ApiOperation(value = "奖励费用发布", notes = "获取奖励费用发布", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<Object> release(@RequestBody CommonVO<Long> feeRewardCommonVO) {
        LOGGER.info("FeeRewardController.release param: {}", feeRewardCommonVO);
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            feeRewardService.release(feeRewardCommonVO.getEntityParams());
        } catch (BaseException e) {
            LOGGER.error("FeeRewardController.release error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeRewardController.release error:{}", e.getMessage());
            result.setMessage("获取奖励费用发布异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @GetMapping("/getFeeRewardListByPayId/{payId}")
    @ApiOperation(value = "奖励费用获取", notes = "奖励费用获取", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<List<FeeRewardVO>> getFeeRewardListByPayId(@PathVariable("payId") Long payId) {
        // getFeeRewardListByPayId
        LOGGER.info("FeeRewardController.getFeeRewardListByPayId payId: {}", payId);
        RestfulResponse<List<FeeRewardVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            result.setData(feeRewardService.getFeeRewardVOListByPayId(payId));
        } catch (BaseException e) {
            LOGGER.error("FeeRewardController.getFeeRewardListByPayId error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeRewardController.getFeeRewardListByPayId error:{}", e.getMessage());
            result.setMessage("获取奖励费用异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }

    @PostMapping(value = "/closeReward")
    @ApiOperation(value = "奖励费用的关闭", notes = "奖励费用的关闭", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<Object> closeReward(@RequestBody CommonVO<Long> feeRewardCommonVO) {
        LOGGER.info("FeeRewardController.release param: {}", feeRewardCommonVO);
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "关闭成功", null);
        try {
            if (Objects.isNull(feeRewardCommonVO)) {
                throw new BaseException("请选择数据");
            }
            if (StringUtils.isBlank(feeRewardCommonVO.getTypeVal())) {
                throw new BaseException("请输入关闭原因");
            }
            if (CollectionUtils.isEmpty(feeRewardCommonVO.getEntityParams())) {
                throw new BaseException("请选择数据");
            }
            List<String> resultMessageList = feeRewardService.closeReward(feeRewardCommonVO.getEntityParams(), feeRewardCommonVO.getTypeVal());
            if (!CollectionUtils.isEmpty(resultMessageList)) {
                result.setMessage("失败的数据");
                result.setData(resultMessageList);
                result.setCode(-2);
            }
        } catch (BaseException e) {
            LOGGER.error("FeeRewardController.closeReward error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeRewardController.closeReward error:{}", e.getMessage());
            result.setMessage("获取奖励费用关闭异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }
}

